<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css" />
<script type="text/javascript" th:src="@{/js/jquery.js}"></script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>采购单管理</li>
    <li>修改</li>
  </ul>
</div>
<div class="formbody">
    <form th:action="@{/purchase/update}" method="post">
  <div class="formtitle"><span>采购单信息</span></div>
  <ul class="forminfo">
    <li>
      <label>采购单编号</label>
      <input name="" type="text" value="CG201711180001" readonly="readonly" class="roinput" />
      <i>自动生成不能编辑</i>
    </li>
    <li>
      <label>采购时间</label>
      <input name="" type="text" value="2019-11-18 15:36:10" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建人</label>
      <input name="" type="text" value="马云" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>审核状态</label>
      <input type="text" value="未审核" readonly="readonly" class="roinput" />
      <input name="" type="hidden" value="1" />
      <i>初始状态，不能编辑</i>
    </li>
    <li>
      <label>总金额</label>
      <input name="totalMoney" type="text" th:value="${byPid.totalMoney}" readonly="readonly" class="roinput" />
       <i>自动运算，不能编辑</i>
    </li>
    <li>
      <input type="button" value="新增" class="smallbtn" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="删除" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="submit" value="保存" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="返回" class="smallbtn" onclick="history.go(-1)"/>
    </li>
  </ul>
  <table class="tablelist">
      <thead>
        <tr>
          <th>序号</th>
          <th>品牌</th>
          <th>类型</th>
          <th>型号</th>
          <th>厂商</th>
          <th>数量</th>
          <th>单位</th>
          <th>单价</th>
          <th>金额</th>
        </tr>
      </thead>
      <tbody>
      	<tr>
          <td>1</td>
          <td>
              <select  name="cBrands" onchange="getbrands(this.value)">
                  <option value="">请选择</option>
                  <option th:each="br:${cBrands}" th:value="${br.brandId}"
                        th:selected="${byPid.brandName==br.brandName}">[[${br.brandName}]]</option>
              </select>
          </td>
          <td>
              <select  name="cTypes" onchange="getProducts(this.value)">
                  <option th:value="${cProduct.cType.typeId}" th:text="${cProduct.cType.typeName}">请选择</option>

              </select>
          </td>
          <td>
              <select  name="productId" onchange="ajaxPrice(this.value)">
                  <option th:value="${cProduct.productId}" th:text="${cProduct.productModel}">请选择</option>
              </select>
          </td>
          <td>
              <select  name="cFirms">
                  <option th:value="${cProduct.cfirm.firmId}" th:text="${cProduct.cfirm.firmName}">请选择</option>

              </select>
          </td>
            <td><input type="text" name="count" onblur="sum()" th:value="${byPid.count}"/></td>
            <td>台</td>
            <td><input type="text" name="purchasePrice" onblur="sum()" th:value="${byPid.purchasePrice}"/></td>
            <td><input type="text" name="money" th:value="${byPid.totalMoney}"/></td>
        </tr>
      </tbody>
  </table>
        <input type="hidden" name="purchaseId" th:value="${byPid.purchaseId}">
        <input type="hidden" name="detailsId" th:value="${byPid.detailsId}">
    </form>

</div>

<script type="text/javascript">
    // 计算金额
    function sum(){
        var count = $("input[name=count]").val();  //商品数量
        var purchasePrice = $("input[name=purchasePrice]").val();  //商品价格
        if(count!=null){
            $("input[name=money]").val(count*purchasePrice);
            $("input[name=totalMoney]").val(count*purchasePrice);

        }
    }

    // 异步 得出商品价格
    function ajaxPrice(productId){
        $.get("/erp/purchase/ajaxPrice",{"productId":productId},function (data) {
            $("input[name=purchasePrice]").val(data.productPrice);
            var str = "<option value='"+data.cfirm.firmId+"'>"+data.cfirm.firmName+"</option>";

            $("select[name=cFirms]").html("");
            $("select[name=cFirms]").append(str);
        },"json");
        sum();
    }


    // 异步得到商品
    function getProducts(typeId) {
        var cBrands = $("select[name=cBrands]").val();
        // console.log("类型id："+typeId+",品牌id："+cBrands);
        var $pro = $("select[name=productId]");
        $.get("/erp/rkWarehouse/ajaxPro",{"typeId":typeId,"brandId":cBrands},function (data) {
            var str = "<option value=\"\">请选择</option>";
            $.each(data,function (i, e) {
                str += "<option value='"+e.productId+"'>"+e.productModel+"</option>";
            })
            $pro.html("");
            $pro.append(str);
        },"json");
    }

    // 异步获取 类型
    function getbrands(num) {
        $.get("/erp/purchase/cproduct/ajaxtypes",{"brandid":num},function (data) {
            //alert(typeof(data));
            $("select[name=cTypes]").html("");
            var options="<option value='0'>请选择</option>";
            $.each(data,function (i,e) {
                options+="<option value='"+e.typeId+"'>"+e.typeName+"</option>";
            });
            $("select[name=cTypes]").append(options);
        },"json");
    }


    $('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
